package net.toby200.canalbuddy.notices.model;


import android.os.Parcel;
import android.os.Parcelable;

import java.util.logging.Logger;


public final class Notice implements Parcelable {

    private static final Logger LOG = Logger.getLogger(Notice.class.getName());

    private long id; // long id needed for SqlLite key
	private String title;
    private String waterway;
	private String fromDate;
	private String toDate;
	private String type;
	private String description;
	private String locationDetail;	
    private Location startLoc;
    private Location endLoc;
    private long lastUpdated;

	
	public Notice(long id, String title, String waterway, String fromDate, String toDate, String type, String description,
			String locationDetail, Location startLoc, Location endLoc, long lastUpdated) {
		super();
        this.id = id;
		this.title = title;
		this.waterway = waterway;
		this.fromDate = fromDate;
		this.toDate = toDate;
		this.type = type;
		this.description = description;
		this.locationDetail = locationDetail;
		this.startLoc = startLoc;
		this.endLoc = endLoc;
        this.lastUpdated = lastUpdated;
	}

    public long getId() {
        return id;
    }

	public String getTitle() {
		return title;
	}

	public String getWaterway() {
		return waterway;
	}

	public String getFromDate() {
		return fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public String getType() {
		return type;
	}

	public String getDescription() {
		return description;
	}

	public String getLocationDetail() {
		return locationDetail;
	}

	public Location getStartLoc() {
		return startLoc;
	}

	public Location getEndLoc() {
		return endLoc;
	}

    public long getLastUpdated() {
        return lastUpdated;
    }

    // setters for use by greenDAO
    public void setId(long id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setWaterway(String waterway) {
        this.waterway = waterway;
    }

    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }

    public void setToDate(String toDate) {
        this.toDate = toDate;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setLocationDetail(String locationDetail) {
        this.locationDetail = locationDetail;
    }

    public void setStartLoc(Location startLoc) {
        this.startLoc = startLoc;
    }

    public void setEndLoc(Location endLoc) {
        this.endLoc = endLoc;
    }

    public void setLastUpdated(long lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Notice{");
        sb.append("title='").append(title).append('\'');
        sb.append(", waterway='").append(waterway).append('\'');
        sb.append(", fromDate='").append(fromDate).append('\'');
        sb.append(", toDate='").append(toDate).append('\'');
        sb.append(", type='").append(type).append('\'');
        sb.append(", description='").append(description).append('\'');
        sb.append(", locationDetail='").append(locationDetail).append('\'');
        sb.append(", startLoc=").append(startLoc);
        sb.append(", endLoc=").append(endLoc);
        sb.append(", lastUpdated=").append(lastUpdated);
        sb.append('}');
        return sb.toString();
    }

    @Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((endLoc == null) ? 0 : endLoc.hashCode());
		result = prime * result + ((fromDate == null) ? 0 : fromDate.hashCode());
		result = prime * result + ((locationDetail == null) ? 0 : locationDetail.hashCode());
		result = prime * result + ((startLoc == null) ? 0 : startLoc.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result = prime * result + ((toDate == null) ? 0 : toDate.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result + ((waterway == null) ? 0 : waterway.hashCode());
		return result;
	}

	@Override
    // compares all fields except lastUpdated
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Notice other = (Notice) obj;
        if (id != other.id) {
            return false;
        }
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description)) {
            LOG.info("Description updated for " + title + " now=" + description + ", previous="+other.description);
            return false;
        }
		if (endLoc == null) {
			if (other.endLoc != null)
				return false;
		} else if (!endLoc.equals(other.endLoc))
			return false;
		if (fromDate == null) {
			if (other.fromDate != null)
				return false;
		} else if (!fromDate.equals(other.fromDate))
			return false;
		if (locationDetail == null) {
			if (other.locationDetail != null)
				return false;
		} else if (!locationDetail.equals(other.locationDetail)) {
            LOG.info("locationDetail updated for " + title + " now=" + locationDetail + ", previous=" + other.locationDetail);
            return false;
        }
		if (startLoc == null) {
			if (other.startLoc != null)
				return false;
		} else if (!startLoc.equals(other.startLoc))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (toDate == null) {
			if (other.toDate != null)
				return false;
		} else if (!toDate.equals(other.toDate)) {
            LOG.info("toDate updated for " + title + " now=" + toDate + ", previous=" + other.toDate);
            return false;
        }
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		if (waterway == null) {
			if (other.waterway != null)
				return false;
		} else if (!waterway.equals(other.waterway))
			return false;
		return true;
	}

    public Notice() {
    }



    public Notice(Parcel p) {
        id = p.readLong();
        title = p.readString();
        waterway = p.readString();
        type = p.readString();
        fromDate = p.readString();
        toDate = p.readString();
        locationDetail = p.readString();
        description = p.readString();
        startLoc =(Location) p.readParcelable(Location.class.getClassLoader());
        endLoc = (Location) p.readParcelable(Location.class.getClassLoader());
        lastUpdated = p.readLong();
    }


    @Override
    public void writeToParcel(Parcel p, int flags) {
        p.writeLong(id);
        p.writeString(title);
        p.writeString(waterway);
        p.writeString(type);
        p.writeString(fromDate);
        p.writeString(toDate);
        p.writeString(locationDetail);
        p.writeString(description);
        p.writeParcelable(startLoc, 0);
        p.writeParcelable(endLoc, 0);
        p.writeLong(lastUpdated);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<Notice> CREATOR = new Parcelable.Creator<Notice>() {

        @Override
        public Notice createFromParcel(Parcel source) {
            return new Notice(source);
        }

        @Override
        public Notice[] newArray(int size) {
            return new Notice[size];
        }
    };
}
